
var agentGrade=0;
$(function(){
	member=getEntity("memberInfo");
	console.log(member)
	if(member){
		if(member.mImg){
			$("#imgSpan").prepend('<img src="'+baseFile+member.mImg+'" id="mImg" alt="" />')
		}else{
			$("#imgSpan").prepend('<img src="'+rootPath+'/images/header.png" id="mImg" alt="" />')
		}
		$("#mId").val(member.mId);
		$("#companyName").val(member.companyName);
		$("#linkman").val(member.linkman);
		$("#phone").val(member.phone);
		$("#agentGrade").val(member.agentGrade);
		if(member.businessLicence){
			$(".upload_pic span").prepend('<img id="businessLicence" src="'+baseFile+member.businessLicence+'" alt="营业执照" />');
		}else{
			$(".upload_pic span").prepend('<img id="businessLicence" src="'+rootPath+'/images/yun.png" alt="" />');
		}
		$("#businessLicence").css('width','120px').css('height','120px');
	}else{
		window.location.href=rootPath+"/";
	}
	if(member.agentGrade){
		agentGrade=member.agentGrade;
	}
	$.ajax({
		type:"post",
		url:rootPath+"/member/selectAllGrade",
		data:{'agentGrade':agentGrade},
		//dataType:"json",
		success:function(data){
			if(data.resultSuccess){
				var agentGradeList=data.data;
				var str='<option>--请选择--</option>';
				$.each(agentGradeList,function(i,item){
					//<option>--请选择--</option>
					if(item.grade==agentGrade){
						str+='<option value="'+item.grade+'" selected>'+item.gradeName+'</option>';
					}else{						
						str+='<option value="'+item.grade+'">'+item.gradeName+'</option>';
					}
				})
				$("#agentGrade").empty();
				$("#agentGrade").append(str);
			}else{
				$("#msg").text(data.resultDesc);
				//layer.msg(result.stockMessage,{offset: ['40%', '40%'],title: ['提醒', 'font-size:18px;'],icon: 1,time:100000});
			}
		}
	});
	$("#phone").change(function(){
		    var  mPhone=/^[1][0-9]{10}$/;
	        var phone=document.getElementById("phone").value;
	        if(!mPhone.test(phone)){
				layer.msg("请输入11位正确的手机号!");
             	$(this).val("");
	        } 
	});

	$("#confirm").click(function(){
		var mId=$("#mId").val();
		var companyName=$("#companyName").val();
		var phone=$("#phone").val();
		if(!phone){
			layer.msg("手机号不能为空");
        	return false;
		}
		var linkman=$("#linkman").val();
		var agentGrade=$("#agentGrade").val();
		$("#mPhone").html("绑定手机："+member.mPhone+"<a href=''>修改手机</a>");
		
		$.ajaxFileUpload({
			  url:rootPath+"/member/insertAgent",
			  type:'post',
			  data:{'mId':mId,'companyName':companyName,'phone':phone,'linkman':linkman,'agentGrade':agentGrade},
		      secureuri:false,  
		      fileElementId:"agentImg",//文件选择框的id属性  
		      dataType: 'JSON',   //json 
		      success: function (data) {
		    	var reg = /<pre.+?>(.+)<\/pre>/g; 
		    	var result = data.match(reg); 
		    	data = RegExp.$1;
		    	data=toJson(data);
		    	if(data.resultSuccess){
		    		layer.msg(data.resultDesc,{offset: ['40%', '40%'],/*title: ['提醒', 'font-size:18px;'],*/icon: 1,time:3000});
		    	}else{
					//layer.msg(data.resultDesc,{offset: ['40%', '40%'],title: ['提醒', 'font-size:18px;'],icon: 2,time:3000});
		    		layer.msg(data.resultDesc,{offset: ['40%', '40%'],/*title: ['提醒', 'font-size:18px;'],*/icon: 2,time:30000});
		    	}
		     }
		});
	});
});

function setImagePreview() { 
	var docObj=document.getElementById("agentImg"); 
	var imgObjPreviewBig=document.getElementById("businessLicence"); 
	if(docObj.files && docObj.files[0]){ 
		//火狐下，直接设img属性
	//	imgObjPreview.style.display = 'block'; 
		imgObjPreviewBig.style.width = '110px'; 
		imgObjPreviewBig.style.height = '110px'; 
		//imgObjPreview.style.margin-left='18px'; 
		//imgObjPreview.src = docObj.files[0].getAsDataURL(); 
		//火狐7以上版本不能用上面的getAsDataURL()方式获取，需要一下方式
		imgObjPreviewBig.src = window.URL.createObjectURL(docObj.files[0]); 
	}else{ 
	//IE下，使用滤镜
		docObj.select(); 
		var imgSrc = document.selection.createRange().text; 
		var localImagId = document.getElementById("localImag"); 
		//必须设置初始大小
		localImagId.style.width = "300px"; 
		localImagId.style.height = "120px"; 
	//图片异常的捕捉，防止用户修改后缀来伪造图片
		try{ 
			localImagId.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)"; 
			localImagId.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgSrc; 
		}catch(e){ 
			alert("您上传的图片格式不正确，请重新选择!"); 
			return false;  
		}
	}
}	